<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CrossScreenDrag</title>
    <style>
        body {
            overflow: hidden;
        }

        .box {
            position: absolute;
            width: 100px;
            height: 200px;
            background: #ccc;
            cursor: move;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>

        const channel = new BroadcastChannel('boxmove')
        channel.onmessage = (e) => {
            const clientPoints = screenToClient(e.data.screenX, e.data.screenY)
            box.style.left = clientPoints.clientX + 'px';
            box.style.top = clientPoints.clientY + 'px';
        }

        function barHeight() {
            return window.outerHeight - window.innerHeight
        }

        function clientToScreen(x, y) {
            const screenX = x + window.screenX
            const screenY = y + window.screenY + barHeight()
            return { screenX, screenY }
        }
        function screenToClient(x, y) {
            const clientX = x - window.screenX
            const clientY = y - window.screenY - barHeight()
            return { clientX, clientY }
        }

        const box = document.querySelector('.box')
        box.onmousedown = (e) => {
            let x = e.pageX - box.offsetLeft;
            let y = e.pageY - box.offsetTop;

            window.onmousemove = (e) => {
                const cx = e.pageX - x;
                const cy = e.pageY - y;

                box.style.left = cx + 'px';
                box.style.top = cy + 'px';

                const screenPoints = clientToScreen(cx, cy)
                channel.postMessage(screenPoints)
            }
            window.onmouseup = () => {
                window.onmousemove = null;
                window.onmouseup = null;
            }
        }


    </script>
</body>

</html>